package com.zzyl.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WechatServiceImpl implements WechatService {

    @Value("${zzyl.wechat.appid}")
    private String appid;
    @Value("${zzyl.wechat.secret}")
    private String secret;

    // 获取openid的url地址
    private static String OPENIDURL="https://api.weixin.qq.com/sns/jscode2session";
    // 获取tokende url地址
    private static String TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token";
    // 获取手机号的url地址
    private static String PHONE_URL="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    // 两个方法都需要这两个参数，所以提出来一个方法
    public Map getBaseParam(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid );
        paramMap.put("secret", secret);
        return paramMap;
    }

    @Override //获取openid
    public String getOpenid(String code) {
        Map paramMap = getBaseParam();
        paramMap.put("grant_type", "authorization_code");
        paramMap.put("js_code", code);

        String result = HttpUtil.get(OPENIDURL, paramMap);
        String openid = null;
        try {
            JSONObject jsonObject = JSONUtil.parseObj(result);
            openid = (String)jsonObject.get("openid");
        } catch (Exception e) {
           throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }

        return openid;
    }



    @Override//获取真实的手机号
    public String getPhone(String code) {


        Map paramMap = new HashMap();
        paramMap.put("code", code);


        String token = getToken(code);
        log.warn("获取手机号的url地址："+PHONE_URL+ token);

        HttpResponse httpResponse = HttpUtil.createRequest(Method.POST, PHONE_URL + token).body(JSONUtil.toJsonStr((paramMap))).execute();
        String phone = null;
        try {
            log.warn("返回的结果："+httpResponse.body());
            JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
            //String pathPhone = jsonObject.getByPath("phone_info.phoneNumber").toString();
            //System.out.println("根据path获取到的手机号"+pathPhone);
            phone = ((JSONObject)jsonObject.get("phone_info")).get("phoneNumber").toString();
            // phone = (String)jsonObject.get("phone_info.phoneNumber");
        } catch (Exception e) {
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
        return phone;
    }

    //获取真实的手机号之前需要token
    private String getToken(String code) {
        Map paramMap = getBaseParam();
        paramMap.put("grant_type", "client_credential");

        String result = HttpUtil.get(TOKEN_URL, paramMap);
        String token = null;
        try {
            JSONObject jsonObject = JSONUtil.parseObj(result);
            token = (String)jsonObject.get("access_token");
        } catch (Exception e) {
            throw new BaseException(BasicEnum.GET_TOKEN_ERROR);
        }

        log.warn("获取到的token："+token);

        return token;
    }
}
